Breadth-first search is a graph traversal algorithm that starts traversing the graph from the root node and explores all the neighboring nodes.
Then, it selects the nearest node and explores all the unexplored nodes. While using BFS for traversal, any node in the graph can be considered as the root node.
It is a recursive algorithm to search all the vertices of a tree or graph data structure. 
steps involved in BFS algorithm are:
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS = 1) and set their STATUS = 2
Step 6: EXIT

CODE:

import java.io.*;    
import java.util.*;    
public class BFSTraversal    
{    
    private int vertex;        
    private LinkedList<Integer> adj[];     
    private Queue<Integer> que;              
    BFSTraversal(int v)    
    {    
        vertex = v;    
        adj = new LinkedList[vertex];    
        for (int i=0; i<v; i++)    
        {    
            adj[i] = new LinkedList<>();    
        }    
        que = new LinkedList<Integer>();    
    }    
    void insertEdge(int v,int w)    
    {    
        adj[v].add(w);         
    }    
    void BFS(int n)    
    {    
        boolean nodes[] = new boolean[vertex];         
        int a = 0;    
        nodes[n]=true;                      
        que.add(n);         
        while (que.size() != 0)    
        {    
            n = que.poll();            
            System.out.print(n+" ");       
            for (int i = 0; i < adj[n].size(); i++)     
            {    
                a = adj[n].get(i);    
                if (!nodes[a])          
                {    
                    nodes[a] = true;    
                    que.add(a);    
                }    
            }      
        }    
    }    
    public static void main(String args[])    
    {    
        BFSTraversal graph = new BFSTraversal(10);    
        graph.insertEdge(0, 1);    
        graph.insertEdge(0, 2);    
        graph.insertEdge(0, 3);    
        graph.insertEdge(1, 3);    
        graph.insertEdge(2, 4);  
        graph.insertEdge(3, 5);       
        graph.insertEdge(3, 6);    
        graph.insertEdge(4, 7);    
        graph.insertEdge(4, 5);    
        graph.insertEdge(5, 2);    
        graph.insertEdge(6, 5);    
        graph.insertEdge(7, 5);  
        graph.insertEdge(7, 8);   
        System.out.println("Breadth First Traversal is");    
        graph.BFS(2);    
    }    
}    
